<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!-- <el-form-item label="標題" prop="quoteTitle">
        <el-input
          v-model="queryParams.quoteTitle"
          placeholder="請輸入標題"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="報價編碼" prop="quoteCode">
        <el-input
          v-model="queryParams.quoteCode"
          placeholder="請輸入報價編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="報價人ID" prop="quoterId">
        <el-input
          v-model="queryParams.quoterId"
          placeholder="請輸入報價人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="報價人" prop="quoterName">
        <el-input
          v-model="queryParams.quoterName"
          placeholder="請輸入報價人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="報價時間" prop="quoteTime">
        <el-date-picker
          clearable
          v-model="queryParams.quoteTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="請選擇報價時間"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="不含稅金額" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="請輸入不含稅金額"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="含稅金額" prop="taxAmount">
        <el-input
          v-model="queryParams.taxAmount"
          placeholder="請輸入含稅金額"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="預約ID" prop="reservationId">
        <el-input
          v-model="queryParams.reservationId"
          placeholder="請輸入預約ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="預約編碼" prop="reservationCode">
        <el-input
          v-model="queryParams.reservationCode"
          placeholder="請輸入預約編碼"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="報價有效期" prop="dueDate">
        <el-date-picker
          clearable
          v-model="queryParams.dueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="請選擇報價有效期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="入賬銀行賬號" prop="inBankNo">
        <el-input
          v-model="queryParams.inBankNo"
          placeholder="請輸入入賬銀行賬號"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入賬銀行名稱" prop="inBankName">
        <el-input
          v-model="queryParams.inBankName"
          placeholder="請輸入入賬銀行名稱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:quote:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:quote:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:quote:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:quote:export']"
          >導出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="quoteList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        show-overflow-tooltip
        width="180"
        label="報價編碼"
        align="center"
        prop="quoteCode"
      />
      <el-table-column
        show-overflow-tooltip
        width="130"
        label="標題"
        align="center"
        prop="quoteTitle"
      />
      <el-table-column
        show-overflow-tooltip
        label="報價人"
        align="center"
        prop="quoterName"
      />
      <el-table-column
        show-overflow-tooltip
        label="報價時間"
        align="center"
        prop="quoteTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.quoteTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        width="100"
        label="不含稅金額"
        align="center"
        prop="quoteAmount"
      />
      <el-table-column
        show-overflow-tooltip
        label="含稅金額"
        align="center"
        prop="quoteTaxAmount"
      />
      <el-table-column
        show-overflow-tooltip
        width="180"
        label="預約編碼"
        align="center"
        prop="reservationCode"
      />
      <el-table-column
        show-overflow-tooltip
        width="100"
        label="報價單狀態"
        align="center"
        prop="quoteStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.quote_status"
            :value="scope.row.quoteStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="報價有效期"
        align="center"
        prop="dueDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.dueDate, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        width="100"
        label="入賬銀行賬號"
        align="center"
        prop="inBankNo"
      />
      <el-table-column
        show-overflow-tooltip
        width="100"
        label="入賬銀行名稱"
        align="center"
        prop="inBankName"
      />
      <el-table-column
        show-overflow-tooltip
        label="備註"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        width="160"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <!-- SendQuote -->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.quoteStatus!='10'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:quote:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
              :disabled="scope.row.quoteStatus!='10'"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:quote:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >詳情</el-button
          >
          <div>
            <el-button
              style="padding: 5px"
              size="mini"
              :disabled="scope.row.quoteStatus != '30'"
              type="primary"
              v-hasPermi="['biz:quote:invoicing']"
              @click="GoToInvoice(scope.row)"
              >開發票</el-button
            >
            <el-button
              style="padding: 5px"
              size="mini"
              type="primary"
              :disabled="scope.row.quoteStatus != '30'"
              v-hasPermi="['biz:quote:send']"
              @click="SendQuote(scope.row)"
              >發送</el-button
            >
            <el-button
              style="padding: 5px"
              size="mini"
              type="primary"
              :disabled="scope.row.quoteStatus == '10'"
              v-hasPermi="['biz:quote:preview']"
              @click="Download(scope.row)"
              >預覽</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改報價單对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="標題" prop="quoteTitle">
          <el-input v-model="form.quoteTitle" placeholder="請輸入標題" />
        </el-form-item>
        <el-form-item label="報價編碼" prop="quoteCode">
          <el-input v-model="form.quoteCode" placeholder="請輸入報價編碼" />
        </el-form-item>
        <el-form-item label="報價人ID" prop="quoterId">
          <el-input v-model="form.quoterId" placeholder="請輸入報價人ID" />
        </el-form-item>
        <el-form-item label="報價人" prop="quoterName">
          <el-input v-model="form.quoterName" placeholder="請輸入報價人" />
        </el-form-item>
        <el-form-item label="報價時間" prop="quoteTime">
          <el-date-picker
            clearable
            v-model="form.quoteTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇報價時間"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="不含稅金額" prop="amount">
          <el-input v-model="form.amount" placeholder="請輸入不含稅金額" />
        </el-form-item>
        <el-form-item label="含稅金額" prop="taxAmount">
          <el-input v-model="form.taxAmount" placeholder="請輸入含稅金額" />
        </el-form-item>
        <el-form-item label="預約ID" prop="reservationId">
          <el-input v-model="form.reservationId" placeholder="請輸入預約ID" />
        </el-form-item>
        <el-form-item label="預約編碼" prop="reservationCode">
          <el-input
            v-model="form.reservationCode"
            placeholder="請輸入預約編碼"
          />
        </el-form-item>
        <el-form-item label="報價有效期" prop="dueDate">
          <el-date-picker
            clearable
            v-model="form.dueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="請選擇報價有效期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入賬銀行賬號" prop="inBankNo">
          <el-input v-model="form.inBankNo" placeholder="請輸入入賬銀行賬號" />
        </el-form-item>
        <el-form-item label="入賬銀行名稱" prop="inBankName">
          <el-input
            v-model="form.inBankName"
            placeholder="請輸入入賬銀行名稱"
          />
        </el-form-item>
        <el-form-item label="刪除標記" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="請輸入刪除標記" />
        </el-form-item>
        <el-form-item label="備註" prop="remark">
          <el-input v-model="form.remark" placeholder="請輸入備註" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">確 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import {
  listQuote,
  getQuote,
  delQuote,
  addQuote,
  updateQuote,
  DownQuote,
  SendQuote,
} from "@/api/biz/quote";

export default {
  name: "Quote",
  dicts: ["sys_normal_disable", "quote_source", "quote_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中數組
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 顯示搜索条件
      showSearch: true,
      // 总条數
      total: 0,
      // 報價單表格數据
      quoteList: [],
      // 弹出层標题
      title: "",
      // 是否顯示弹出层
      open: false,
      // 查询參數
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        quoteTitle: null,
        quoteCode: null,
        quoterId: null,
        quoterName: null,
        quoteTime: null,
        amount: null,
        taxAmount: null,
        reservationId: null,
        reservationCode: null,
        quoteStatus: null,
        dueDate: null,
        payTime: null,
        inBankNo: null,
        inBankName: null,
        delFlag: null,
        orderByColumn: "update_time",
        isAsc: "desc",
      },
      // 表单參數
      form: {},
      // 表单校验
      rules: {
        quoteCode: [
          { required: true, message: "報價編碼不能為空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询報價單列表 */
    getList() {
      this.loading = true;
      listQuote(this.queryParams).then((response) => {
        this.quoteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        quoteTitle: null,
        quoteCode: null,
        quoterId: null,
        quoterName: null,
        quoteTime: null,
        amount: null,
        taxAmount: null,
        reservationId: null,
        reservationCode: null,
        quoteStatus: null,
        dueDate: null,
        payTime: null,
        inBankNo: null,
        inBankName: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中數据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.open = true;
      // this.title = "添加報價單";
      this.$router.push({ path: "/quote/quote/add" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.$router.push({
        path: "/quote/quote/edit",
        query: { id: id, quoteSource: row.quoteSource },
      });
    },
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      this.$router.push({
        path: "/quote/quote/detail",
        query: { id: id, quoteSource: row.quoteSource },
      });
    },
    // 開發票
    GoToInvoice(item) {
      let query_params = {
        id: item.id,
        // quoteCode:item.quoteCode,
        // customerId:item.customerId,
        // customerCode:item.customerCode,
        // customerName:item.customerName,
      };
      this.$router.push({
        path: "/invoice/invoice/add",
        query: query_params,
      });
    },
    Download(item) {
      console.log(window.location.origin);
      // http://47.92.243.248
      axios({
        url: window.location.origin + "/prod-api/biz/quote/preview/" + item.id,
        method: "GET",
        responseType: "blob", // 重要
      })
        .then((response) => {
          console.log(response.data);
          const blob = new Blob([response.data], { type: "application/pdf" });
          const url = URL.createObjectURL(blob);
          window.open(url);
        })
        .catch((error) => {
          console.error("Error downloading the file:", error);
        });
    },
    SendQuote(item) {
      SendQuote(item.id).then((response) => {
        console.log(response);
        if (response.code == 200) {
          this.$modal.msgSuccess("發送成功");
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQuote(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuote(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否確認删除報價單編號為"' + ids + '"的數据项？')
        .then(function () {
          return delQuote(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 導出按钮操作 */
    handleExport() {
      this.download(
        "biz/quote/export",
        {
          ...this.queryParams,
        },
        `quote_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
